package com.by.toolkit.exceptions;

import cn.hutool.core.util.StrUtil;
import com.by.toolkit.enums.ErrorCodeEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * @author Tian Wei
 * @version 1.0
 * @date 2023/12/26 5:11 PM
 */
@Getter
@NoArgsConstructor
public class ServiceException extends RuntimeException {

    private String code;
    private String data;
    private String msg;

    public ServiceException(String msg) {
        super(msg);
        this.code = ErrorCodeEnum.BIZ_FAIL.getErrorCode();
        this.msg = msg;
    }

    public ServiceException(String code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public ServiceException(String code, String msg, String data) {
        super(msg);
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public ServiceException(String code, String msg, Object... args) {
        super(msg);
        this.code = code;
        this.msg = StrUtil.format(msg, args);
    }

    public ServiceException(ErrorCodeEnum codeEnum) {
        super(codeEnum.getErrorMessage());
        this.code = codeEnum.getErrorCode();
        this.msg = codeEnum.getErrorMessage();
    }

    public ServiceException(ErrorCodeEnum codeEnum, String data) {
        super(codeEnum.getErrorMessage());
        this.code = codeEnum.getErrorCode();
        this.msg = codeEnum.getErrorMessage();
        this.data = data;
    }

    @Override
    public String getMessage() {
        return this.getMsg();
    }

}
